package com.hyphenate.chatuidemo.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.CodeBean;
import com.joplus.housekeeper.bean.ServiceList;
import com.joplus.housekeeper.network.RequestHelper;
import com.joplus.housekeeper.network.TextCallback;
import com.joplus.housekeeper.services.XmppService;
import com.joplus.housekeeper.ui.activity.MainActivity;
import com.joplus.housekeeper.ui.view.dialog.ServiceSelectDialog;
import com.joplus.housekeeper.utils.StringUtils;
import com.joplus.housekeeper.utils.SystemBarTintManager;
import com.joplus.housekeeper.utils.T;
import com.joplus.housekeeper.utils.UMengUtils;
import com.joplus.housekeeper.xmpp.SmackManager;


import java.util.HashMap;
import java.util.Map;

/**
 * 开屏页
 */
public class SplashActivity extends BaseActivity {
    private static final int sleepTime = 500;
    Bundle bundle;
    String TAG = "SplashActivity";
    private EditText et_password;
    private TextView et_name;


    @Override
    protected void onCreate(Bundle arg0) {
        Log.e(TAG, "onCreate: ");
        int statusBarColor = android.R.color.transparent;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        //使StatusBarTintView 和 actionbar的颜色保持一致，风格统一。
        tintManager.setStatusBarTintResource(statusBarColor == -1 ? android.R.color.transparent : statusBarColor);
        // 设置状态栏的文字颜色
        tintManager.setStatusBarDarkMode(true, this);

        setContentView(R.layout.em_activity_splash);


        super.onCreate(arg0);


        et_name = (TextView) findViewById(R.id.et_name);
        et_password = (EditText) findViewById(R.id.et_password);

        et_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestHelper.getInstance().allService(SplashActivity.this, new TextCallback() {
                    @Override
                    public void onFailure(String responseString) {

                    }

                    @Override
                    public void onSuccess(Object responseString) {
                        ServiceList responseString1 = (ServiceList) responseString;
                        if (responseString1.getCode().equals("2000")) {
                            final ServiceSelectDialog serviceSelectDialog = new ServiceSelectDialog(SplashActivity.this, responseString1.getData());
                            serviceSelectDialog.show(SplashActivity.this);
                            serviceSelectDialog.setOnPayClickListener(new ServiceSelectDialog.PayClick() {
                                @Override
                                public void onclick(String string) {
                                    et_name.setText(string);
                                    serviceSelectDialog.dismiss();
                                }
                            });

                        } else {
                            T.showShort(SplashActivity.this, responseString1.getMessage());
                        }
                    }
                });
            }
        });


        findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(et_name.getText().toString()) || StringUtils.isEmpty(et_password.getText().toString())) {

                    T.showShort(SplashActivity.this, "请检查参数");
                    return;
                }
                login(et_name.getText().toString(), et_password.getText().toString());

            }
        });


    }


    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }


    public void stopAnim() {


    }

    @Override
    protected void onPause() {

        super.onPause();
        UMengUtils.pagePause(0);
    }


    private SmackManager instance;
    Map<String, String> attributes = new HashMap<>();

    private void login(final String name, final String password) {

        RequestHelper.getInstance().imLogin(name, password, new TextCallback() {
            @Override
            public void onFailure(String responseString) {

            }

            @Override
            public void onSuccess(Object responseString) {
                CodeBean responseString1 = (CodeBean) responseString;
                if (!responseString1.getCode().equals("2000")) {
                    T.showShort(SplashActivity.this, responseString1.getMessage());
                    return;
                }
                Intent intent = new Intent(SplashActivity.this, XmppService.class);
                Bundle bundle = new Bundle();

                bundle.putString("userName", name);
                bundle.putString("password", password);
                intent.putExtras(bundle);
                startService(intent);

                Intent intent1 = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent1);

                finish();
            }
        });

      /*  attributes.put("name", name);
        Observable.create(new ObservableOnSubscribe<SmackManager>() {
            @Override
            public void subscribe(ObservableEmitter<SmackManager> e) throws Exception {
                instance = SmackManager.getInstance();
                instance.getConnection();
                e.onNext(instance);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<SmackManager>() {
            @Override
            public void accept(SmackManager smackManager) throws Exception {
                LoginResult login = instance.login(name, password);
                if (login.isSuccess()) {
                    XmppUser user = login.getUser();
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    intent.putExtra("service", user);
                    startActivity(intent);
                    finish();
                } else {
                    T.showShort(SplashActivity.this, login.getErrorMsg());
                }
            }
        });*/
      /*Observable.create(new ObservableOnSubscribe<SmackManager>() {


            @Override
            public void subscribe(ObservableEmitter<SmackManager> e) throws Exception {
                instance = SmackManager.getInstance();
                instance.getConnection();
                e.onNext(instance);
            }
        }).subscribeOn(Schedulers.io()).map(new Function<SmackManager, Boolean>() {
            @Override
            public Boolean apply(SmackManager o) throws Exception {
                boolean b = o.registerUser(name, password, attributes);

                return b;
            }
        }).observeOn(Schedulers.io()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                LoginResult login = instance.login(name, password);
                if (login.isSuccess()) {
                    XmppUser user = login.getUser();
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    intent.putExtra("service", user);
                    startActivity(intent);
                    finish();
                } else {
                    T.showShort(SplashActivity.this, login.getErrorMsg());
                }



              *//*  instance.addMessageListener();
                instance.addFileListener();
                instance.offlineMessage(instance.getConnection());*//*


            }
        })*/
        ;

    }
}
